home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / filename_match.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-07  |  2.9 KB  |  114 lines

  1. //
  2. // "$Id: filename_match.cxx,v 1.5 1999/01/07 21:22:28 mike Exp $"
  3. //
  4. // Pattern matching routines for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. /* Adapted from Rich Salz. */
  27. #include <FL/filename.H>
  28. #include <ctype.h>
  29.  
  30. int filename_match(const char *s, const char *p) {
  31.   int matched;
  32.  
  33.   for (;;) {
  34.     switch(*p++) {
  35.  
  36.     case '?' :    // match any single character
  37.       if (!*s++) return 0;
  38.       break;
  39.  
  40.     case '*' :    // match 0-n of any characters
  41.       if (!*p) return 1; // do trailing * quickly
  42.       while (!filename_match(s, p)) if (!*s++) return 0;
  43.       return 1;
  44.  
  45.     case '[': {    // match one character in set of form [abc-d] or [^a-b]
  46.       if (!*s) return 0;
  47.       int reverse = (*p=='^' || *p=='!'); if (reverse) p++;
  48.       matched = 0;
  49.       char last = 0;
  50.       while (*p) {
  51.     if (*p=='-' && last) {
  52.       if (*s <= *++p && *s >= last ) matched = 1;
  53.       last = 0;
  54.     } else {
  55.       if (*s == *p) matched = 1;
  56.     }
  57.     last = *p++;
  58.     if (*p==']') break;
  59.       }
  60.       if (matched == reverse) return 0;
  61.       s++; p++;}
  62.     break;
  63.  
  64.     case '{' : // {pattern1|pattern2|pattern3}
  65.     NEXTCASE:
  66.     if (filename_match(s,p)) return 1;
  67.     for (matched = 0;;) {
  68.       switch (*p++) {
  69.       case '\\': if (*p) p++; break;
  70.       case '{': matched++; break;
  71.       case '}': if (!matched--) return 0; break;
  72.       case '|': case ',': if (matched==0) goto NEXTCASE;
  73.       case 0: return 0;
  74.       }
  75.     }
  76.     case '|':    // skip rest of |pattern|pattern} when called recursively
  77.     case ',':
  78.       for (matched = 0; *p && matched >= 0;) {
  79.     switch (*p++) {
  80.     case '\\': if (*p) p++; break;
  81.     case '{': matched++; break;
  82.     case '}': matched--; break;
  83.     }
  84.       }
  85.       break;
  86.     case '}':
  87.       break;
  88.  
  89.     case 0:    // end of pattern
  90.       return !*s;
  91.  
  92. #ifdef WIN32
  93.     case '\\':    // quote next character
  94.       if (*p) p++;
  95.       if (*s++ != *(p-1)) return 0;
  96.       break;
  97.     default:
  98.       if (tolower(*s) != tolower(*(p-1))) return 0;
  99.       s++;
  100. #else
  101.     case '\\':    // quote next character
  102.       if (*p) p++;
  103.     default  :
  104.       if (*s++ != *(p-1)) return 0;
  105.       break;
  106. #endif
  107.     }
  108.   }
  109. }
  110.  
  111. //
  112. // End of "$Id: filename_match.cxx,v 1.5 1999/01/07 21:22:28 mike Exp $".
  113. //
  114.